<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="ru.niir.goodfil.ui.StorekeeperOrdersBean" %>
<%@ page import="ru.niir.goodfil.db.Order" %>
<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Архив</title>
    <link rel="stylesheet" href="resources/css/main.css"/>

    <%--
        <script type="text/javascript" src="resources/js/fixedtableheader.js"></script>
    --%>
    <script type="text/javascript">
        function PrintContent(documentId)
        {
            var DocumentContainer = document.getElementById(documentId);
            var WindowObject = window.open("", "PrintWindow",
                    "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
            var htmlToPrint = '' +
                    '<style type="text/css">' +
                    'table th, table td {' +
                    'border:1px solid #000;' +
                    'padding:0.5em;' +
                    '}' +
                    '</style>';
            htmlToPrint += DocumentContainer.outerHTML;
            WindowObject.document.writeln(htmlToPrint);
            WindowObject.document.close();
            WindowObject.focus();
            WindowObject.print();
            WindowObject.close();
        }
    </script>
    <style type="text/css">
        .rich-table-subheadercell-rotate {
            -webkit-transform: rotate(-90deg);
            -moz-transform: rotate(-90deg);
            -o-transform: rotate(-90deg);
            -ms-transform: rotate(-60deg);
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
            white-space: normal;
            height: 100px;
            width: 2px;
            align:right;
        }

        .rich-table-subheadercell {
            white-space: normal;
            height: 120px;
        }

        td{
            height: 20px;
        }

        th{
            height: 100px;
        }

        table{
            border-collapse: collapse;
        }
        @media print {
            #buttons {display: none}
        }
    </style>
</head>
<body>
<f:view>

<a4j:form id="jsForm" prependId="false">

    <a4j:jsFunction name="refresh" action="#{StorekeeperDeliveryReport.reloadData}" limitToList="true"
                    reRender="hForm"/>

    <a4j:jsFunction name="addOrderId" action="#{StorekeeperDeliveryReport.addOrderId}" limitToList="true">
        <a4j:actionparam assignTo="#{StorekeeperDeliveryReport.orderId}"/>
    </a4j:jsFunction>

</a4j:form>

<jsp:include page="parts/version.jsp"/>
<jsp:include page="StorageMenu.jsp"/>

<h:form id="dateForm">
    <div align="center">
        <h:panelGrid columns="5" cellpadding="10" cellspacing="10">
            <h:outputText value="Доставка заказов на: " styleClass="b"/>
            <rich:calendar value="#{StorekeeperDeliveryReport.date}" locale="RU_ru"
                           datePattern="dd.MM.yyyy" style="width:80px;padding-left:4px;" id="calBegdate"/>

            <a4j:commandButton value="Выбрать"
                               action="#{StorekeeperDeliveryReport.reloadData}"
                               reRender="dateForm, hForm"
                               style="width:150px;font-weight:bold;"/>
        </h:panelGrid>

    </div>
</h:form>

<h:form id="hForm" prependId="false">
    <jsp:include page="messages.jsp"/>
    <jsp:include page="status.jsp"/>
    <h:panelGroup layout="block" styleClass="" style="padding:5px; position: fixed;" >
        <div style="display:table; width:100%; margin-bottom: 10px;" id="buttons">
            <div align="left" style="display:table-cell;">
                <a4j:commandButton value="Суммировать объединенное"
                                   title="Выберите клиентов для объединения сборки"
                                   onclick="Richfaces.showModalPanel('sum')"
                                   rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}"
                                   reRender="modalForm"/>
            </div>
        </div>
    </h:panelGroup>
    <rich:dataTable
            id="ordersForBuid"
            value="#{StorekeeperDeliveryReport.items}"
            var="order"
            styleClass="full"
            cellpadding="0"
            rowKeyVar="row"
            >

        <rich:column>
            <f:facet name="header">
                <h:outputText value="№"/>
            </f:facet>
            <h:outputText value="#{row + 1}. "/>
        </rich:column>

        <rich:column rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}"
                     headerClass="rich-table-subheadercell-rotate"
                     width="10"
                     title="Объединить выбранные">
            <f:facet name="header">
                <h:outputText value="Объед. выбр."/>
            </f:facet>
            <div align="center"><h:selectBooleanCheckbox immediate="true"
                                                         onchange="addOrderId(#{order.id})"/></div>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Номер ИС"/>
            </f:facet>
            <h:outputText value="#{order.id}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Счет 1С"/>
            </f:facet>
            <h:outputText value="#{order.c1Num}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Дата создания"/>
            </f:facet>
            <h:outputText value="#{order.creationDateAsString}"/>
        </rich:column>

        <rich:column sortable="true" sortBy="#{order.deliveryDate}">
            <f:facet name="header">
                <h:outputText value="Дата отгрузки запланированная"/>
            </f:facet>
            <h:outputText value="#{order.deliveryDateAsString}"/>
        </rich:column>

        <rich:column sortable="true" sortBy="#{order.deliveryDate}">
            <f:facet name="header">
                <h:outputText value="Дата отгрузки фактическая"/>
            </f:facet>
            <h:outputText value="#{order.factDeliveryDateAsString}"/>
        </rich:column>

        <rich:column >
            <f:facet name="header">
                <h:outputText value="Менеджер"/>
            </f:facet>
            <h:outputText value="#{order.manager.fio}"/>
        </rich:column>

        <rich:column filterBy="#{order.client.companyName}" filterEvent="onblur">
            <f:facet name="header">
                <h:outputText value="Клиент"/>
            </f:facet>
            <h:outputText value="#{order.client.companyName}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Статус клиента"/>
            </f:facet>
            <h:outputText value="#{order.client.clientStateAsString}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Сумма заказа"/>
            </f:facet>
            <h:outputText value="#{order.factSumAsString}"/>
        </rich:column>

        <rich:column>
            <f:facet name="header">
                <h:outputText value="Тип доставки"/>
            </f:facet>
            <h:outputText value="#{order.deliveryTypeAsString}"/>
        </rich:column>
        <f:facet name="footer">
            <rich:columnGroup>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column/>
                <rich:column>
                    <h:outputText value="Итого: #{StorekeeperDeliveryReport.deliveryOrdersSum}"/>
                </rich:column>
                <rich:column/>
            </rich:columnGroup>
        </f:facet>
    </rich:dataTable>
</h:form>

<rich:modalPanel id="sum" height="300">
    <h:form id="modalForm" styleClass="scroll_div">
        <div id="modalPrint">
            <rich:dataTable id="itemsSumTable"
                            value="#{StorekeeperDeliveryReport.orderItemsSum}"
                            var="orderitem"
                            rowKeyVar="row"
                            styleClass="full"
                    >
                <rich:column>
                    <f:facet name="header">
                        <h:outputText value="Код товара"/>
                    </f:facet>
                    <h:outputText value="#{orderitem.good.code}"/>
                </rich:column>
                <rich:column>
                    <f:facet name="header">
                        <h:outputText value="Количество"/>
                    </f:facet>
                    <h:outputText value="#{orderitem.count}"/>
                </rich:column>
            </rich:dataTable>
        </div>

    </h:form>
    <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0">
        <a4j:commandButton value="Печать"
                           styleClass="btnsz"
                           onclick="PrintContent('modalPrint');"/>
        <a4j:commandButton value="Закрыть"
                           styleClass="btnsz"
                           onclick="refresh();Richfaces.hideModalPanel('sum');"
                />
    </h:panelGrid>
</rich:modalPanel>
</f:view>
</body>
</html>

